//
//  YHIMUIKitMessageCell.m
//  YHChat
//
//  Created by apple on 2019/2/25.
//  Copyright © 2019 yinhe. All rights reserved.
//

#import "YHIMUIKitMessageCell.h"
#import "YHIMUIKitHelper.h"
#import "YHIMUIKitMessageCellData.h"

@interface YHIMUIKitMessageStateNode()
@property (nonatomic, strong) ASDisplayNode *indicatorNode;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
@property (nonatomic, strong) ASImageNode *errorImageNode;
@end

@implementation YHIMUIKitMessageStateNode
- (instancetype)init
{
    self = [super init];
    if (self) {
        __weak typeof(self) weakSelf = self;
        
        self.indicatorNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView * _Nonnull{
            weakSelf.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            return weakSelf.indicatorView;
        }];
        [self addSubnode:self.indicatorNode];
        
        self.errorImageNode = [[ASImageNode alloc] init];
        self.errorImageNode.image = [UIImage imageNamed:YHIMUIKit_Resource(@"msg_error")];
        self.errorImageNode.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubnode:self.errorImageNode];
        
        self.indicatorNode.backgroundColor = [UIColor redColor];
        self.errorImageNode.backgroundColor = [UIColor orangeColor];
    }
    return self;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize{
    return [ASOverlayLayoutSpec overlayLayoutSpecWithChild:self.indicatorNode overlay:self.errorImageNode];
}

@end


/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
@interface YHIMUIKitMessageCell()
@property (nonatomic, strong) ASNetworkImageNode *headImageNode;
@property (nonatomic, strong) ASTextNode *nameNode;
@property (nonatomic, strong) YHIMUIKitMessageStateNode *stateNode;
@end


@implementation YHIMUIKitMessageCell

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        
        self.headImageNode = [[ASNetworkImageNode alloc] init];
        self.headImageNode.contentMode = UIViewContentModeScaleAspectFill;
        self.headImageNode.style.preferredSize = YHIMUIKit_HeadSize;
        self.headImageNode.cornerRadius = YHIMUIKit_HeadSize.height / 2.0;
        self.headImageNode.clipsToBounds = YES;
        [self addSubnode:self.headImageNode];
        
        self.nameNode = [[ASTextNode alloc] init];
        self.nameNode.maximumNumberOfLines = 1;
        self.nameNode.style.width = ASDimensionMake(YHIMUIKit_ScreenWidth - 60 - YHIMUIKit_HeadSize.width);
        [self addSubnode:self.nameNode];
        
        self.stateNode = [[YHIMUIKitMessageStateNode alloc] init];
        self.stateNode.style.preferredSize = YHIMUIKit_StateSize;
        [self addSubnode:self.stateNode];
        
        self.headImageNode.backgroundColor = [UIColor redColor];
        self.nameNode.backgroundColor = [UIColor orangeColor];
        self.stateNode.backgroundColor = [UIColor purpleColor];
    }
    return self;
}

- (void)setCellData:(YHIMUIKitMessageCellData *)cellData{
    _cellData = cellData;
    
    self.nameNode.attributedText = [YHIMUIKitHelper attributedStringWithText:_cellData.name color:YHIMUIKit_RGB(51, 51, 51) font:[UIFont systemFontOfSize:13] kern:0 lineSpacing:0 alignment:NSTextAlignmentLeft];
    
    if (_cellData.status == YHIMUIKitMsgStatus_Ready) {
        [self.stateNode.indicatorView stopAnimating];
        self.stateNode.indicatorNode.hidden = YES;
        self.stateNode.errorImageNode.hidden = YES;
    } else if (_cellData.status == YHIMUIKitMsgStatus_Sending) {
        [self.stateNode.indicatorView startAnimating];
        self.stateNode.indicatorNode.hidden = NO;
        self.stateNode.errorImageNode.hidden = YES;
    } else if (_cellData.status == YHIMUIKitMsgStatus_Success) {
        [self.stateNode.indicatorView stopAnimating];
        self.stateNode.indicatorNode.hidden = YES;
        self.stateNode.errorImageNode.hidden = YES;
    } else if (_cellData.status == YHIMUIKitMsgStatus_Fail) {
        [self.stateNode.indicatorView stopAnimating];
        self.stateNode.indicatorNode.hidden = YES;
        self.stateNode.errorImageNode.hidden = NO;
    }
}

@end
